<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Beritakegiatan_model extends MY_Model {

    function __construct() {
        parent::__construct("activity_news", "id", Array(
            "activity_id" => "Kegiatan_model",
            "content_id" => "Content_model"
        ));
        $this->delAttribute = "is_deleted";
    }

    public function create($kegId, $title, $text, $tags = Array(), $pics = Array()) {
        $this->load->model("User_model");
        $curUserId = $this->curUserId();
        $roles = $this->User_model->fetchRoles($curUserId);
        $isAdmin = in_array("kegiatan_auditor", $roles);
        $kegiatan = $this->Kegiatan_model->findById($kegId, true, 2);
        $result = NULL;
        if ($kegiatan != NULL) {
            $contentId = $this->Content_model->create($title, $text, $kegiatan->content_id->thumbnail_img->id, $kegiatan->content_id->banner_img->id, $this->curUserId(), $tags, $pics);
            $result = parent::create(Array(
                        "content_id" => $contentId,
                        "activity_id" => $kegiatan->id,
                        "is_deleted" => 0,
                        "is_approved" => $isAdmin
            ));
        }
        return $result;
    }

    protected function read($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $results = parent::read($where, $fetchForeign, $depth, $page, $limit);

        foreach ($results as $result) {
            if (is_object($result->content_id)) {
                $shortTrimmed = preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text);
                $shortTrimmed = substr($shortTrimmed, 0, 240);
                $shortTrimmed .= " <a href='" . site_url("kegiatan/detail/" . $result->activity_id->id . "/" . $result->id) . "'>Lebih lanjut...</a>";
                $result->content_id->short = $shortTrimmed;
            }
        }

        return $results;
    }

    public function searchByPoster($idPoster, $fetchForeign = false, $depth = 1) {
        $query = $this->db->query("SELECT activity_news.id FROM activity_news, content WHERE activity_news.content_id = content.id AND activity_news.is_deleted = 0 AND content.poster_id = " . $idPoster . " ORDER BY activity_news.is_approved ASC, content.posted DESC LIMIT 0, 10");
        $resArr = Array();

        foreach ($query->result() as $res) {
            array_push($resArr, $this->findById($res->id, $fetchForeign, $depth));
        }

        return $resArr;
    }

    public function fetchRecent($page = 0, $limit = 5) {
        $query = $this->db->query("SELECT activity_news.id FROM activity_news, content WHERE activity_news.content_id = content.id AND activity_news.is_deleted = 0 AND activity_news.is_approved = 1 ORDER BY content.posted DESC LIMIT " . ($page * $limit) . ", " . $limit);
        $resArr = Array();

        foreach ($query->result() as $res) {
            array_push($resArr, $this->findById($res->id, true, 3));
        }

        return $resArr;
    }

    public function fetchRecentPics($page = 0, $limit = 5) {
        $this->db->select("content_pics.image_id as id");
        $this->db->distinct();
        $this->db->from("activity_news, content, content_pics");
        $this->db->where("activity_news.content_id = content.id");
        $this->db->where("content.id = content_pics.content_id");
        $this->db->where("activity_news.is_approved", true);
        $this->db->where("activity_news.is_deleted", false);
        $this->db->order_by("content_pics.image_id", "desc");
        $this->db->limit($limit, ($page * $limit));

        $query = $this->db->get();
        $resArr = Array();

        $this->load->model("Image_model");
        foreach ($query->result() as $res) {
            array_push($resArr, $this->Image_model->findById($res->id));
        }

        return $resArr;
    }

}